<?php

namespace App\Http\Controllers;

use App\Services\Entity\PasswordService;
use App\Services\Entity\UserService;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    /**
     * @var UserService
     */
    protected $userService;

    /**
     * @var PasswordService
     */
    protected $passwordService;

    /**
     * @var Request
     */
    protected $request;

    public function __construct(
        UserService $userService,
        PasswordService $passwordService,
        Request $request
    )
    {
        $this->userService = $userService;
        $this->passwordService = $passwordService;
        $this->request = $request;
    }

    protected function getPageParams() :array
    {
        $page = (int)$this->request->input("page",1);
        if ($page <= 0) {
            $page = 1;
        }
        $pageSize = (int)$this->request->input("pageSize",10);
        if ($pageSize <= 0) {
            $pageSize = 10;
        }

        if ($pageSize > 100) {
            $pageSize = 100;
        }

        return [$page, $pageSize];
    }

    protected function getFromSizeParams(): array
    {
        $page = (int)$this->request->input('page', 1);
        if ($page <= 0) {
            $page = 1;
        }
        $pageSize = (int)$this->request->input('pageSize', 10);
        if ($pageSize <= 0) {
            $pageSize = 10;
        }
        if ($pageSize > 100) {
            $pageSize = 100;
        }
        $from = ($page - 1) * $pageSize;
        $size = $pageSize;
        return [$from, $size];
    }
}
